package com.cqq.print.base.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.cqq.common.core.base.BaseController;
import com.cqq.common.core.bean.PageInfoBean;
import com.cqq.print.base.service.PProducePlanService;
import com.cqq.print.base.service.bean.PProducePlanBean;
import com.cqq.pro.util.ResponseUtil;

/**
 * 2015/10/18
 *
 * @author chenqq
 */
@Controller(value = "producePlanController")
@RequestMapping(value = { "producePlan" }, method = {
		RequestMethod.POST,
		RequestMethod.GET })
public class PProducePlanController extends BaseController{

	@Autowired
	private PProducePlanService pProducePlanService;
	
	/**
	 * 查询生产方式列表
	 * @param request
	 * @param out
	 * @return
	 */
	@RequestMapping("list")
	public String toList(HttpServletRequest request, ModelMap out) {
		
		return "base/producePlan";
		
	}
	
	/**
	 * 查询生产方式列表(异步)
	 * @param request
	 * @param out
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("listAjax")
	public String toListAjax(HttpServletRequest request, ModelMap out) throws UnsupportedEncodingException {
		String name = request.getParameter("name");		
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		Map<String,Object> map = new HashMap<String, Object>();
		
		if(name!=null&&!name.trim().equals("")){
			name = URLDecoder.decode(name, "utf-8");
			map.put("proPlanName", name);
		}
		if(currentPage==null||currentPage.trim().length()<1){
			currentPage = "1";
		}
		if(pageSize==null||pageSize.trim().length()<1){
			pageSize = "20";
		}
		
		int offset = (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize);
		map.put("orderClause", "pp.create_date desc");
		map.put("pageSize", Integer.parseInt(pageSize));
		map.put("offset", offset);
		
		PageInfoBean<PProducePlanBean> page = pProducePlanService.queryPProducePlanByPage(map);
		page.setPageNo(Integer.parseInt(currentPage));
		page.setPageSize(Integer.parseInt(pageSize));
		
		out.put("page", page);
		return "base/producePlan";
		
	}
	
	/**
	 * 新建生产方式
	 * @param request
	 * @param out
	 * @return
	 */
	@RequestMapping("submitPProducePlan")
	public void submitPProducePlan(PProducePlanBean ppBean, HttpServletRequest request,HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try{
			int ppId = pProducePlanService.insertSelective(ppBean);
			if(ppId>0){
				json.put("result", "success");
			}else{
				json.put("result", "faild");
				json.put("msg", "生产方式对象为空，请重新添加！");
			}
		}catch(Exception e){
			json.put("msg", "程序异常，请联系运维人员！");
			log.error(e.getMessage());
		}
		
		ResponseUtil.outJsonString(response, json);
	}
	
	/**
	 * 更新生产方式状态
	 * @param request
	 * @param out
	 * @return
	 */
	@RequestMapping("updateStatus")
	public void updateStatus(PProducePlanBean ppBean, HttpServletRequest request,HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try{
			int ppId = pProducePlanService.updateBySelective(ppBean);
			if(ppId>0){
				json.put("result", "success");
			}else{
				json.put("result", "faild");
				json.put("msg", "生产方式对象为空！");
			}
		}catch(Exception e){
			json.put("msg", "程序异常，请联系运维人员！");
			log.error(e.getMessage());
		}
		
		ResponseUtil.outJsonString(response, json);
	}
	
	
	/**
	 * 根据Id查询生产方式
	 * @param request
	 * @param out
	 * @return
	 */
	@RequestMapping("findProducePlanById")
	public void findProducePlanById(Integer proPlanId, HttpServletRequest request,HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try{
			if(proPlanId!=null){
				PProducePlanBean proPlanBean = pProducePlanService.findProPlanById(proPlanId);
				json.put("result", "success");
				json.put("proPlanBean", proPlanBean);
			}else{
				json.put("result", "faild");
				json.put("msg", "生产方式对象为空！");
			}
		}catch(Exception e){
			json.put("msg", "程序异常，请联系运维人员！");
			log.error(e.getMessage());
		}
		ResponseUtil.outJsonString(response, json);
	}
	
}
